package com.ruoyi.project.user.company.Comtroller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.company.domain.Company;
import com.ruoyi.project.user.company.domain.Planner;
import com.ruoyi.project.user.company.service.CompanyService;
import com.ruoyi.project.user.company.service.PlannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * company控制器
 */
@Controller
@RequestMapping("/user/company")
public class CompanyController extends BaseController {
    private String prefix = "user/company";

    @Autowired
    private CompanyService companyService;
    @Autowired
    private IUserService userService;
    @Autowired
    private PlannerService plannerService;

    @RequiresPermissions("user:company:view")
    @GetMapping("")
    public String showView(){
        return prefix +"/company";
    }

    @RequiresPermissions("user:company:add")
    @GetMapping("/add")
    public String add(){
        return prefix +"/add";
    }

    @RequiresPermissions("user:company:edit")
    @GetMapping("/edit/{companyId}")
    public String edit(@PathVariable Integer companyId, Model model){
        Company companyById = companyService.findCompanyById(companyId);
        User user = userService.selectUserById(companyById.getCompanyUserId());
        model.addAttribute("company",companyById);
        model.addAttribute("password",user.getPassword());
        return prefix + "/edit";
    }

    @RequiresPermissions("user:company:planner")
    @GetMapping("/planner/{companyId}")
    public String planner(@PathVariable Integer companyId, Model model){
        Company companyById = companyService.findCompanyById(companyId);
        model.addAttribute("companyName",companyById.getCompanyName());
        model.addAttribute("companyId",companyId);
        return prefix + "/planner";
    }










    @RequiresPermissions("user:company:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Company company){
        startPage();
        List<Company> list = companyService.findAll(company);
        return getDataTable(list);
}

    @RequiresPermissions("user:company:view")
    @PostMapping("/listPlanner/{companyId}")
    @ResponseBody
    public TableDataInfo listPlanner(@PathVariable Integer companyId){
        startPage();
        Planner planner = new Planner();
          planner.setPlannerCompanyId(companyId);
        List<Planner> all = plannerService.findAll(planner);
        return getDataTable(all);

    }
    @RequiresPermissions("user:company:add")
    @PostMapping("/checkName")
    @ResponseBody
    public int checkName(String companyName){
        return companyService.checkName(companyName);
    }
    @RequiresPermissions("user:company:add")
    @PostMapping("/checkEmail")
    @ResponseBody
    public int checkEmail(String companyEmail){
        return companyService.checkEmail(companyEmail);
    }
    @RequiresPermissions("user:company:add")
    @PostMapping("/checkPhone")
    @ResponseBody
    public int checkPhone(String companyPhone){
        return companyService.checkPhone(companyPhone);
    }
    @RequiresPermissions("user:company:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Company company, String password) {
        return toAjax(companyService.addCompany(company, password));
    }

    @RequiresPermissions("user:company:update")
    @PostMapping("/updateState")
    @ResponseBody
    public AjaxResult updateState(Integer companyState,Integer companyId) {
        return toAjax(companyService.updateState(companyState, companyId));
    }

    @RequiresPermissions("user:company:update")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult updateCompany(Company company) {
        return toAjax(companyService.updateCompany(company));
    }

    @RequiresPermissions("user:company:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeCompany(Integer[] ids) {
        return toAjax(companyService.removeCompany(ids));
    }

    @RequiresPermissions("user:company:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Company company){
        List<Company> list = companyService.export(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "婚庆公司数据");
    }

}
